<template>
  <div class="serviceGiftEdit detailView pd-20">
    <Form :model="item"
          label-position="right"
          :label-width="100"
          :rules="rules"
          ref="form">
      <FormItem label="名称"
                prop="name">
        <Input v-model="item.name"
               type="text" />
      </FormItem>
      <!--<FormItem label="类型" prop="type" required>-->
      <!--<Select v-model="item.type" style="width:200px">-->
      <!--<Option v-for="(type,index) in typeList" :value="type.code" :key="index">{{ type.name }}</Option>-->
      <!--</Select>-->
      <!--</FormItem>-->
      <FormItem label="原价"
                prop="originalPrice"
                required>
        <InputNumber v-model="item.originalPrice"></InputNumber>
      </FormItem>
      <FormItem label="现价"
                prop="price"
                required>
        <InputNumber v-model="item.price"></InputNumber>
      </FormItem>
      <FormItem label="有效天数"
                prop="validDate"
                required>
        <InputNumber v-model="item.validDate"></InputNumber>
      </FormItem>
      <FormItem label="内容"
                prop="content"
                required>
        <Input v-model="item.content"
               type="textarea"
               :rows="4" />
      </FormItem>
      <FormItem label="图片">
        <upload ref="upload"
                :fileUploadAction="fileUploadAction"
                :multiple="multiple"
                :imageList="item.imageUrls"
                @setUrl="setUrl"
                @removeUrl="removeUrl"
                :uploadListLength="uploadListLength"></upload>
      </FormItem>
      <FormItem label="URL">
        <Input v-model="item.url" />
      </FormItem>
    </Form>
    <div class="operationView">
      <Button type="primary"
              class="operation_btn"
              @click="handleCancle">取消</Button>
      <Button type="primary"
              class="operation_btn"
              @click="handleSave">保存</Button>
    </div>
  </div>
</template>

<script>
  import { servicePackage } from '@/api/codeName'
  import Upload from '@/base/upload/upload'
  export default {
    name: "serviceGiftEdit",
    data () {
      let validate = (rule, value, callback) => {
        if (!value && value != 0) {
          callback(new Error('请输入数字'))
        } else {
          callback()
        }
      }
      return {
        item: {
          id: 0,
          name: '',
          type: '1',
          originalPrice: null,
          price: null,
          validDate: null,
          content: '',
          imageUrls: [],
          url: ''
        },
        fileUploadAction: this.api.file.upload,
        multiple: false,
        uploadListLength: 6,
        typeList: servicePackage.serviceGiftMgt.type,
        rules: {
          name: [
            { required: true, message: '请输入服务包礼品名称', trigger: 'blur' }
          ],
          originalPrice: [
            { validator: validate, trigger: 'blur' }
          ],
          price: [
            { validator: validate, trigger: 'blur' }
          ],
          validDate: [
            { validator: validate, trigger: 'blur' }
          ]
        }
      }
    },
    mounted () {
      if (this.$route.query.id) {
        this._getViewById()
      }
    },
    methods: {
      handleCancle () {
        this.$router.back(-1)
      },
      handleSave () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let obj = Object.assign({}, this.item)
            obj.imageUrls = obj.imageUrls.join(',');
            this.ajax({
              type: this.api.serviceGiftEdit.type,
              url: this.api.serviceGiftEdit.url,
              data: obj,
              success: (data) => {
                this.$Message.success('保存成功');
                this.handleCancle()
              },
              error: (data) => {
                this.$Message.error(data.message);
              }
            })
          }
        })
      },
      setUrl (file) {
        this.item.imageUrls.push(file.url)
      },
      removeUrl (index) {
        this.item.imageUrls.splice(index, 1)
      },
      _getViewById () {
        this.ajax({
          type: this.api.serviceGiftView.type,
          url: this.api.serviceGiftView.url,
          data: {
            id: this.$route.query.id
          },
          success: (data) => {
            this.item = data.object
            if (this.item.imageUrls) {
              this.item.imageUrls = this.item.imageUrls.split(',')
              this.$refs.upload.setImg(this.item.imageUrls)
            } else {
              this.item.imageUrls = []
            }
          },
          error: (data) => {
            this.$Message.error(data.message);
          }
        })
      }
    },
    components: {
      Upload
    }
  }
</script>

<style lang="scss" type="text/scss" rel="stylesheet/scss">
  .serviceGiftEdit {
  }
</style>
